#include <stdlib.h>
#include <string.h>

#include "sort.h"

static void merge_sort(STR_NUM **nums, int left, int mid, int right)
{
    STR_NUM **t = malloc((right - left + 1)*sizeof(STR_NUM *));
    int i = left, j = mid + 1, k = 0;
    while((i <= mid) && (j <= right)){
        if(big_data_cmp(nums[i]->s, nums[j]->s) > 0){
            t[k++] = nums[j++];
        }else{
            t[k++] = nums[i++];
        }
    }
    while(i <= mid){
        t[k++] = nums[i++];
    }
    while(j <= right){
        t[k++] = nums[j++];
    }
    memcpy(nums+left, t, (right - left + 1)*sizeof(STR_NUM *));
    free(t);
}

void merge_process(STR_NUM **nums, int left, int right)
{
    int mid;
    if(left == right){
        return;
    }
    mid = left + ((right - left)/2);
    merge_process(nums, left, mid);
    merge_process(nums, mid + 1, right);
    merge_sort(nums, left, mid, right);
}

